package org.apache.rocketmq.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.time.LocalDateTime;

/**
 * @Description TODO
 * @Author lijing1@wxchina.com
 * @Date 2021/2/4 11:16
 **/

public class SyncProducer {

    public static final String NAME_SRV_ADDR = "localhost:9876";

    public static final String TOPIC = "wuxia-test";


    public static void main(String[] args) throws Exception {
        //Instantiate with a producer group name.
        DefaultMQProducer producer = new
                DefaultMQProducer("wx-producer-group-01");
        // Specify name server addresses.
        producer.setNamesrvAddr(NAME_SRV_ADDR);
        //Launch the instance.
        producer.start();
        int msgSize=2;
        for (int i = 0; i < msgSize; i++) {
            //Create a message instance, specifying topic, tag and message body.
            String msgBodyStr = (LocalDateTime.now() + "-wuxia " +
                    i);
            Message msg = new Message(TOPIC,
                    "tag1",
                    msgBodyStr.getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //Call send message to deliver message to one of brokers.
            SendResult sendResult = producer.send(msg);
            System.out.println("msg:" + new String(msg.getBody()));
            System.out.println("sendResult:" + sendResult);
        }
        //Shut down once the producer instance is not longer in use.
        producer.shutdown();
    }
}
